package com.pig4cloud.pigx.wallet.controller;

import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.utils.BigDecimalUtils;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.user.entity.User;
import com.pig4cloud.pigx.wallet.api.feign.RemoteRippleUserService;
import com.pig4cloud.pigx.wallet.entity.RechargeRecord;
import com.pig4cloud.pigx.wallet.entity.WithdrawalRecord;
import com.pig4cloud.pigx.wallet.utils.WalletEmailSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

/**
 * 测试邮件发送
 *
 * @since 2022/9/27 11:35
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/email")
@Api(value = "email", tags = "测试邮件发送")
public class EmailTriggerController {

	private final WalletEmailSender walletEmailSender;
	private final RemoteRippleUserService remoteRippleUserService;
	private final StringRedisTemplate stringRedisTemplate;

	@ApiOperation(value = "发送充值成功邮件", notes = "发送充值成功邮件")
	@PostMapping("/recharge")
	public R<Boolean> sendRecharge(@RequestBody RechargeRecord rechargeRecord) {
		Long userId = SecurityUtils.getUser().getId();
		User user = remoteRippleUserService.getUserByUserId(userId, SecurityConstants.FROM_IN).getData();

		try {
			walletEmailSender.sendRecharge(BigDecimalUtils.toStrRemoveExtraZero(rechargeRecord.getQuantity()),
					rechargeRecord.getSourceAddress(),
					rechargeRecord.getTransactionHash(),
					user);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		return R.ok();
	}

	@ApiOperation(value = "发送提现成功邮件", notes = "发送提现成功邮件")
	@PostMapping("/withdrawal")
	public R<Boolean> sendWithdrawal(@RequestBody WithdrawalRecord withdrawalRecord) {
		Long userId = SecurityUtils.getUser().getId();
		User user = remoteRippleUserService.getUserByUserId(userId, SecurityConstants.FROM_IN).getData();

		try {
			walletEmailSender.sendWithdrawal(BigDecimalUtils.toStrRemoveExtraZero(withdrawalRecord.getActualWithdrawalAmount()),
					withdrawalRecord.getArrivalAddress(),
					withdrawalRecord.getTransactionHash(),
					user);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		return R.ok();
	}

	@ApiOperation(value = "发送确认提现验证码邮件", notes = "发送确认提现验证码邮件")
	@PostMapping("/sendEmail")
	public R<Boolean> sendEmail(@RequestBody WithdrawalRecord withdrawalRecord) {
		Long userId = SecurityUtils.getUser().getId();
		User user = remoteRippleUserService.getUserByUserId(userId, SecurityConstants.FROM_IN).getData();

		// 六位随机数验证码
		String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
		try {
			walletEmailSender.sendConfirmwithdrawal(BigDecimalUtils.toStrRemoveExtraZero(withdrawalRecord.getActualWithdrawalAmount()),
					withdrawalRecord.getArrivalAddress(),
					code,
					user);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		//向redis里存入数据和设置缓存时间
		stringRedisTemplate.opsForValue().set(
				"wallet:email:code:" + userId,
				code, 60 * 3, TimeUnit.SECONDS);

		return R.ok();
	}
}
